require(["common", "title", "bus_permission", "hash_routie", "coAjax", "queryString", "href", "serverNotify", "moment", "moment-locale-zh-cn"],
    function(jSouper, title, bus_permission, hash_routie, coAjax, QueryString, href, serverNotify, moment) {
        $("body").on("click", ".tab-title", function() {
            $(this).parent().find(".tab-content").toggle(200);
        });
        /*
         * 通用的数据格式化辅助函数
         */
        App.set("$Handle.moment.format_HH_mm", function(time) {
            time = +time;
            return (time && time < Number.MAX_VALUE) ? moment(+time).format("HH:mm") : "00:00";
        });
        App.set("$Handle.lottery.visitor_interval_limit", function _(time) {
            time = +time;
            var value_map = [{
                type: "天 ",
                interval: 24 * 60 * 60 * 1000
            }, {
                type: "小时 ",
                interval: 60 * 60 * 1000
            }, {
                type: "分钟 ",
                interval: 60 * 1000
            }, {
                type: "秒 ",
                interval: 1000
            }];
            var res = "";
            jSouper.forEach(value_map, function(item) {
                var value = time / item.interval;
                if (value > 1) {
                    var int_value = parseInt(value);
                    time -= int_value * item.interval;
                    res += int_value + item.type
                }
                if (value - int_value == 0) { //没有剩余的值， 就不用再算了
                    return false;
                }
            });
            return res;
        });

        /*
         * 商家信息并为其配置HASH路由
         */
        var permission_error_vm = jSouper.parse("<h1>权限不足</h1><android:paperbutton event-click=\"{{'$Event.no_permission.back'}}\"> << 返回</android:paperbutton>")(App);
        App.set("$Event.no_permission.back", function() {
            history.back();
        });
        window.showLoader();
        coAjax.get(appConfig.bus.loginer, function(data) {
            window.busInfo = data.result;

            appConfig.site_info = busInfo;
            appConfig.bus_id = busInfo._id;
            App.set("bus_loginer", busInfo);
            App.set("bus_info", busInfo);
            /*
             * 初始化窗口信息
             */
            title(busInfo.info.title);

            if (busInfo._current_employee) {
                var loginer_perms = [];
                jSouper.forEach(busInfo._current_employee.perms, function(permission_hash) {
                    loginer_perms.push.apply(loginer_perms, bus_permission.hash_to_keys(permission_hash));
                });
                //继承制的权限设置，所以需要过滤父级没有的
                var parent_perms = busInfo.perms || bus_permission.default_pers;
                loginer_perms = jSouper.filter(loginer_perms, function(item) {
                    return jSouper.indexOf(parent_perms, item) !== -1
                });
                App.set("$Cache.perms", loginer_perms);
            } else {
                App.set("$Cache.perms", loginer_perms = busInfo.perms || bus_permission.default_pers);
            }
            /*渲染出导航栏树的数据*/
            var loginer_perms_tree = bus_permission.keys_to_tree(loginer_perms);
            var loginer_perms_renderable = bus_permission.tree_to_renderable(loginer_perms_tree);
            App.set("$Cache.perms_tree", loginer_perms_tree);
            App.set("$Cache.perms_renderable", loginer_perms_renderable);
            hash_routie({
                html_url: "./pages/admin/",
                js_url: "./js/pages/admin/",
                hash_prefix: "admin/:page",
                default_hash: loginer_perms ? (loginer_perms.indexOf("orders") !== -1 ? "orders" : loginer_perms[0]) : "orders",
                teleporter: "right",
                before_handle: function(hash_key, _viewModules) {
                    if (loginer_perms.indexOf(hash_key) === -1 && false) {
                        alert("error", "权限不足")
                        _viewModules[hash_key] = permission_error_vm;
                    }
                }
            });
            window.removeLoader();
        }, function(errorCode, xhr, errorMsg) {
            console.log(errorCode);
            switch (errorCode) {
                case "JSON Parse Error":
                    break;
                default:
                    href.toBusLogin();
            }
        });
        //届时会换成"/"+busInfo._id
        App.set("base_url", "");

        var bus_notify = serverNotify("bus");
        App.set("$Cache.bus_notify", bus_notify);
        App.set("$Event.bus.loginout", function() {
            href.jump("/admin-login.html");
            coAjax.post(appConfig.bus.loginout, function() {
                alert("成功退出登录");
            });
        });

        //空间使用形式
        jSouper.registerHandle("#Space", function(space_type) {
            var res = "";
            if (space_type) {
                jSouper.forEach(appConfig.SPACE_TYPE, function(value, key) {
                    if (value == space_type) {
                        res = key;
                    }
                })
            }
            return res;
        });
        //触发页面init函数
        App.set("$Event.refresh_hash", function() {
            hash_routie.emit(hash_routie._current_key);
            alert("info waiting", "页面数据刷新");
        });
        window.upload_image = function(file, dtd) {
            require(["./js/tools/_image_upload.js"], function(image_upload) {
                image_upload(file, function(err, precentComplete, url) {
                    if (precentComplete == 100) {
                        dtd.resolve(url);
                        console.log("url,", url)
                        alert("success", "上传成功");
                    };
                });
            });
            return dtd;
        };
    });
